package com.srwl.mytx.im.conversation;

import android.content.Context;
import android.content.Intent;
import android.text.TextUtils;
import android.util.Log;

import com.alibaba.fastjson.JSONObject;
import com.srwl.mytx.Constant;
import com.srwl.mytx.R;
import com.srwl.mytx.activity.ConversationActivity.AdvertisementNotifyActivity;
import com.srwl.mytx.dao.DBManager;
import com.srwl.mytx.domain.Advertisement;
import com.srwl.mytx.domain.ConversationData;
import com.srwl.mytx.domain.Message;
import com.srwl.mytx.im.MessageHandler;
import com.srwl.mytx.utils.IDUtils;

public class AdvertisementNotifyConversation extends SystemNotifyConversation {
    private static final String TAG = "AdvertisementMessageCon";

    public AdvertisementNotifyConversation(Context context, ConversationData data, MessageHandler messageHandler, MessageUnReadCountListener unReadCountListener) {
        super(context, data, messageHandler, unReadCountListener);
    }

    @Override
    public void receivedMessage(Message message) {
        Log.d(TAG, "onPushMessage: 收到广告位竞价成功推送消息");
        syncUserInfo();
        Advertisement advertisement = JSONObject.parseObject(message.getExtraData(), Advertisement.class);
        if (advertisement == null) {
            return;
        }
        // 判断保证金是否充足，如果不足，生成保证金不足通知消息
        if (advertisement.getAmount().compareTo(advertisement.getDeposit()) > 0) {
            message.setSubType(Constant.PUSH_MESSAGE_ADVERTISEMENT_DEPOSIT_NOT_ENOUGH);
            message.setId(IDUtils.genOrderItemId());
            saveMessageAndRefreshView(message);
        }
        // 判断是否设置了广告位 ，如果没有，生成设置广告位展示内容的通知消息
        if (TextUtils.isEmpty(advertisement.getVideo()) && TextUtils.isEmpty(advertisement.getPicture())) {
            try {
                //*************切忌************
                // 不能直接在message 上修改后分发出去，必须在一个新的对象上修改
                Message cloneMsg = (Message) message.clone();
                cloneMsg.setSubType(Constant.PUSH_MESSAGE_ADVERTISEMENT_NEED_SET_SHOW_CONTENT);
                cloneMsg.setId(IDUtils.genOrderItemId());
                saveMessageAndRefreshView(cloneMsg);
            } catch (CloneNotSupportedException e) {
                e.printStackTrace();
            }
        }
    }

    /**
     * 系统推送消息未读数，以消息表中查询出来的数量为准，而不是 ConversationData 中记录的未读数
     * 因为推送消息存在覆盖的问题，比如同一个人发送的添加联系人请求，就算重复发送，在message 中，也只会存在一条数据，
     */
    protected void queryUnReadCount() {
        //每次保存消息后，都重新读取通知类型的消息未读数量，因为推送类型的消息id,可能是重复的（比如和某一个人的添加联系人相关的消息或者是某个悬赏主题的任务通知消息），
        // 也许某条信息还是未读状态，就被覆盖了，只有去数据库读取未读数，才能保证正确的通知类型消息的未读总数
        int newUnReadCount = messageHandler.queryUnreadCount(Constant.CONVERSATION_TYPE_ADVERTISEMENT + "");
        if (newUnReadCount > unReadCount && unReadCountListener != null) {
            unReadCountListener.onReadCountAdd(newUnReadCount - unReadCount);
            Log.d(TAG, "queryUnReadCount: 广告推送消息未读数增加" + (newUnReadCount - unReadCount));
            unReadCount = newUnReadCount;
        } else if (newUnReadCount < unReadCount && unReadCountListener != null) {
            unReadCountListener.onReadCountReduce(unReadCount - newUnReadCount);
            Log.d(TAG, "queryUnReadCount: 广告推送消息未读数减少" + (unReadCount - newUnReadCount));
            unReadCount = newUnReadCount;
        }
    }


    public String getTitle() {
        return "广告位通知";
    }

    @Override
    public int getIconResourceId() {
        return R.drawable.tx_advertisement_icon;
    }

    public String getContent() {
        if (lastMessage == null) {
            return "";
        }
        //如果要把通知里面携带的广告位的最新状态，展示到会话页面，得根据里面的广告位状态来确定返回的内容，也许是保证金不足，也许已经充值了。
        if (lastMessage.getSubType() == Constant.PUSH_MESSAGE_ADVERTISEMENT_DEPOSIT_NOT_ENOUGH) {
            return "广告位保证金不足";
        } else if (lastMessage.getSubType() == Constant.PUSH_MESSAGE_ADVERTISEMENT_NEED_SET_SHOW_CONTENT) {
            return "广告位没有设置展示内容";
        }
        return "";
    }

    /**
     * 打开通知页面
     *
     * @param context
     */
    public void openConversation(Context context) {
        if (unReadCount > 0) {
            if (unReadCountListener != null) {
                unReadCountListener.onReadCountReduce(unReadCount);
            }
            messageHandler.setMessageReadByConversationId(cId);
            unReadCount = 0;
        }
        context.startActivity(new Intent(context, AdvertisementNotifyActivity.class));
    }
}
